package com.suning.sawp.in.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.view.AbstractView;

import com.google.gson.ExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 
 * gson视图<br>
 * 拓展GsonView
 * 
 * @author LiuCongwen
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class MyGsonView extends AbstractView {
    private String datePattern = "yyyy-MM-dd HH:mm:ss";
    /** Default content type. Overridable as bean property. */

    private String jsonObjectName;
    
    
    public MyGsonView() {
        super();
    }
    public MyGsonView(String jsonObjectName, ExclusionStrategy excludeStrategy) {
        super();
        this.jsonObjectName = jsonObjectName;
        this.excludeStrategy = excludeStrategy;
    }
    private int responseStatus = HttpStatus.OK.value();
    private ExclusionStrategy excludeStrategy;
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setStatus(getResponseStatus());
        response.setContentType(getContentType());
        GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat(datePattern);
        if (excludeStrategy != null) {
            gsonBuilder.setExclusionStrategies(excludeStrategy);
        }
        Gson gson = gsonBuilder.create();
        String json = gson.toJson(jsonObjectName == null ? model : model.get(jsonObjectName));
        response.getOutputStream().write(json.getBytes("UTF-8"));
        response.getOutputStream().flush();  
        response.getOutputStream().close();

    }
    /**
     * @return the objectName in model
     */
    public String getJsonObjectName() {
        return jsonObjectName;
    }
    
    public void setDatePattern(String pattern){
        this.datePattern = pattern;
    }
    /**
     * @param objectName the objectName in model that will be convert to json, if not set, then convert the hole model to json
     */
    public void setJsonObjectName(String objectName) {
        this.jsonObjectName = objectName;
    }
    /**
     * @return the excludeStrategy
     */
    public ExclusionStrategy getExcludeStrategy() {
        return excludeStrategy;
    }
    /**
     * @param excludeStrategy the excludeStrategy to set
     */
    public void setExcludeStrategy(ExclusionStrategy excludeStrategy) {
        this.excludeStrategy = excludeStrategy;
    }
    /* (non-Javadoc)
     * @see org.springframework.web.servlet.view.AbstractView#getContentType()
     */
    @Override
    public String getContentType() {
        return "application/json;charset=utf-8";
    }
    /**
     * @return the responseStatus
     */
    public int getResponseStatus() {
        return responseStatus;
    }
    /**
     * @param responseStatus the responseStatus to set
     */
    public void setResponseStatus(int responseStatus) {
        this.responseStatus = responseStatus;
    }

}
